package com.jd.ty.dao;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.classic.Session;

import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.pojo.CustomerOrder;

public class OrderDAO extends BaseDAO {

	public OrderDAO() {
		this.setEntityType(CustomerOrder.class);
	}

	public int getOrdersByCustomerId(int id) {
		Session session = hbmTemplate.getSessionFactory().getCurrentSession();
		BigDecimal count =(BigDecimal) session.createSQLQuery("select count(*) from COMPANY_ORDER where CUSTOMER_COMPANY_ID=:id").setInteger("id", id).uniqueResult();
		return count.intValue();
	}

	public void deleteByCompanyId(Long custCompanyId) {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.Order.COMPANY+"." +QueryConstants.CustomerCompany.CUST_COMPANY_ID, custCompanyId, QueryConstants.EQ);
		List<CustomerOrder> orders=getByQuery(hc.getHqlString());
		for (CustomerOrder customerOrder : orders) {
			customerOrder.setCustomer(null);
			customerOrder.setEmployee(null);		}
		hbmTemplate.deleteAll(orders);
	}

}
